﻿$(document).ready(function() {

	$.namespace("def", {
		inherited: true,

		InitialisePage: function() {

			function RenderTabs() {
				$("#loading").hide();
				$("#app_tabs").show();

				// set the tabs
				var tabs = new Ext.TabPanel({
					id: 'apptabs',
					renderTo: 'app_tabs',
					activeTab: 0,
					autoShow: true,
					plain: true,
					defaults: { autoHeight: true },
					items: [
										{ id: 'tabcm', contentEl: 'current_month', title: 'Current Month' },
										{ id: 'tabm', contentEl: 'months', title: 'Months' },
										{ id: 'tabc', contentEl: 'categories', title: 'Categories' },
										{ id: 'tabi', contentEl: 'income', title: 'Income' },
										{ id: 'tabh', contentEl: 'history', title: 'History' }
									],
					listeners: {
						'tabchange': function(tabpanel, tab) {
							// empty all the tabs
							$(".TabContent").empty();

							// only render the appropriate tab content
							switch (tab.contentEl) {
								case 'income':
									$.fn.income.InitialiseControl();
									break;
								case 'categories':
									$.fn.category.InitialiseControl();
									break;
								case 'months':
									$.fn.mth.InitialiseControl();
									break;
								case 'current_month':
									$.fn.currentmonth.InitialiseControl();
									break;
								case 'history':
									$.fn.history.InitialiseControl();
									break;
							}
						}
					}
				});
			}
			// get the id of the month
			$.ajax({
				type: "POST",
				url: 'Data/Months.aspx?xaction=getmonthbydate',
				contentType: "application/json;",
				data: JSON.stringify(new Date()),
				dataType: "json",
				async: false,
				success: function(msg) {
					if (true) { }
					if (msg.SingleData == null) {
						RenderTabs();
					}
					else {

						$.fn.global.CurrentMonth(msg.SingleData);

						// get the month categories for the current month
						$.ajax({
							type: "POST",
							url: 'Data/MthCategories.aspx?xaction=list',
							contentType: "application/json;",
							data: JSON.stringify($.fn.global.CurrentMonth().MthId),
							dataType: "json",
							async: false,
							success: function(msg) {
								if (true) { }
								$.fn.global.CurrentMonthCategories(msg.Data);

								RenderTabs();
							},
							error: function(msg) {
								if (true) { }
								$("#loading").text(msg.Data.Message);
								//alert('An error occured while retreiving the current month.');
							}
						});
					}

				},
				error: function(msg) {
					if (true) { }
					$("#loading").text(msg.Data.Message);
					//alert('An error occured while retreiving the current month.');
				}
			});




		},

		RenderComboBox: function() {

			var exampleData = $.fn.global.Months(true);

			// simple array store
			var store = new Ext.data.JsonStore({
				fields: ['MonthIndex', 'MonthName'],
				data: exampleData
			});

			var comboWithTooltip = new Ext.form.ComboBox({
				//tpl: '<tpl for="."><div ext:qtip="{state}. {nick}" class="x-combo-list-item">{state}</div></tpl>',
				store: store,
				displayField: 'MonthName',
				valueField: 'MonthIndex',
				//				hiddenName: 'SelectedMonth',
				typeAhead: true,
				mode: 'local',
				triggerAction: 'all',
				emptyText: 'Select a state...',
				renderTo: 'cboMonths'
			});
		}
	});

	$.fn.def.InitialisePage();
	//	testing
	//	$.fn.def.RenderComboBox();
});